package com.zhuhjay.service.system.model.vo;

import cn.hutool.core.util.DesensitizedUtil;
import com.zhuhjay.common.base.util.BeanBuilder;
import com.zhuhjay.service.system.model.entity.SystemRole;
import com.zhuhjay.service.system.model.entity.SystemUser;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * @author ZhuHJay
 * @date 2023/2/3 18:19
 */
@Data
public class UserInfoVo {
    private Integer id;
    private String username;
    private String avatar;
    private String nickName;
    private String telephone;
    private String email;
    private Date createTime;
    private Date lastLoginTime;
    private Integer status;

    private Boolean bindWx;

    private SystemRole role;
    private List<PermissionVo> permissions;

    /**
     * 将SystemUser转换为UserInfoVo
     *
     * @param user              SystemUser
     * @param isDesensitization 是否脱敏
     * @return UserInfoVo
     */
    public static UserInfoVo of(SystemUser user, boolean isDesensitization) {
        String telephone, email;
        // 消息脱敏判断
        if (isDesensitization) {
            telephone = DesensitizedUtil.fixedPhone(user.getTelephone());
            email = DesensitizedUtil.email(user.getEmail());
        } else {
            telephone = user.getTelephone();
            email = user.getEmail();
        }
        return BeanBuilder.builder(UserInfoVo::new)
                .with(UserInfoVo::setId, user.getId())
                .with(UserInfoVo::setUsername, user.getUsername())
                .with(UserInfoVo::setAvatar, user.getAvatar())
                .with(UserInfoVo::setNickName, user.getNickName())
                .with(UserInfoVo::setTelephone, telephone)
                .with(UserInfoVo::setEmail, email)
                .with(UserInfoVo::setCreateTime, user.getCreateTime())
                .with(UserInfoVo::setLastLoginTime, user.getLastLoginTime())
                .with(UserInfoVo::setStatus, user.getStatus())
                .build();
    }
}
